/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package snippet.theory2;

/**
 *  Person entity
 * @author hoang.pt
 */
class PersonThread extends Thread {

    int minute;
    
    /**
     * @var ToothPaste
     */
    ToothPaste toothPaste;

    /**
     * @var ToothCup
     */
    ToothCup toothCup;

    /**
     * constructor
     * @param String name
     * @param ToothPaste toothPaste
     * @param ToothCup toothCup
     */
    public PersonThread(String name, int minute, ToothPaste toothPaste, ToothCup toothCup) {
        this.setName(name);
        this.minute = minute;
        this.toothPaste = toothPaste;
        this.toothCup = toothCup;
    }

    /**
     * Clean teeth
     * @author hoang.pt
     * @param minute
     */
    public void doCleanTeeth(int minute) {
        toothCup.takeUp();
        toothPaste.takeUp();

        //teeth clean
        try {
            System.out.println(this.getName()+ " cleaning teeth.");

            for (int i=1; i<=minute; i++) {
				Thread.sleep(1000);
                for (int j=0; j<i; j++) {
                    System.out.print(".");
                }
                System.out.print("\n");
            }

        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }
        
    }

    /**
     * Finish and getout
     * @author hoang.pt
     */
    public void doFinish() {
        toothCup.putDown();
        toothPaste.putDown();

        System.out.println(this.getName()+ " finish.");
    }

    @Override
    public void run() {
        //super.run();
        while(true) {
            this.doCleanTeeth(this.minute);
            this.doFinish();
        }
        
    }
}



/**
 * Main
 * @author hoang.pt
 */
public class BathDemo{

    public static void main(String[] args) {
        //init bath item
        ToothPaste colgate = new ToothPaste();
        ToothCup bilgate = new ToothCup();

        //init thread
        PersonThread father = new PersonThread("father", 5, colgate, bilgate);
        PersonThread son = new PersonThread("son", 2, colgate, bilgate);
        PersonThread girl = new PersonThread("girl", 10, colgate, bilgate);

        father.setPriority(10);
        girl.setPriority(1);
        
        //run
        girl.start();
        son.start();
        father.start();
    }

}
